package com.wiz.weixin.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.weixin.TO.FuellingDataTO;
import com.wiz.weixin.VO.MemberVO;
import com.wiz.weixin.criteria.FuelingSearchCriteriaTO;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.FuellingDataService;
import com.wiz.weixin.service.MemberService;
import com.wiz.weixin.util.PageView;

/**
 * 加油记录
 * @author xxy
 *
 */
@Controller
@RequestMapping("/fuellingData")
public class FuellingDataControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private TencentManager tencentManager;
	@Resource
	private MemberService memberService;
	@Resource
	private FuellingDataService fuellingDataService;
	/**
	 * 跳转
	 */
	@RequestMapping(value="/toRecord")
	public String records(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,
			HttpSession session, Model model) throws JSONException {
		String code = request.getParameter("code");
		try {
			String openid ="ovNRGs8IVk3ctBk5YLcxE45XG50g";//tencentManager.getOpenId(code);
			session.setAttribute("openid", openid);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/fuelling/record";
		
	}
	/**
	 * 查询加油记录
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public String list(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,
			HttpSession session, Model model) throws JSONException {
		PageView pageView = new PageView(pageSize, pageNo);
		try {
			String openid = (String) session.getAttribute("openid");
			
			List<MemberVO> list = memberService.listMemberByOpenId(openid);
			pageView = fuellingDataService.list(pageView,list);
			pageView.setCode(200);
		} catch (Exception e) {
			log.error(e.getMessage());
			pageView.setCode(300);
			e.printStackTrace();
		}
		return gson.toJson(pageView);
		
	}

}
